﻿package mars.handler.testservice;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

// import com.boco.bmdp.eoms.entity.base.CommonSheetResponse;
// import com.boco.bmdp.eoms.entity.base.MsgHeader;
// import com.boco.bmdp.eoms.entity.faultsheet.importFaultDwdotInfoSrv.ImportFaultDwdotInfoSrvRequest;
// import com.boco.bmdp.eoms.service.faultsheet.IFaultSheetProvideSrv;
// import com.boco.commonutil.dateutil.DateUtil;
// import com.boco.commonutil.network.NetworkUtil;
// import com.boco.jl.mobileom.start.activity.Start;
// import com.boco.jl.mobileom.util.BocoApp;
// import com.boco.jl.mobileom.util.MsgHeaderProducer;
// import com.boco.jl.mobileom.util.po.Constant;
// import com.boco.jl.mobileom.util.po.User;
// import com.boco.transnms.server.bo.base.ServiceUtils;

/**
 * 
 * 
 */
public class PollingService extends Service {

	public static final String ACTION = "com.boco.jl.mobileom.service.PollingService";

	// private User user;
	// private String operateUserId;
	// private String operateUserName;
	// private MsgHeader mhr;

	// int i=0;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// BocoApp APP = (BocoApp) this.getApplication();
		// user = APP.getUser();
		// operateUserId = user.getUserId();
		// operateUserName = user.getUserName();
		// mhr = MsgHeaderProducer.produce(operateUserId, operateUserName, 0, 1);

		Log.i("执行开始", "开始轮询");
		new PollingThread().start();
	}

	class PollingThread extends Thread {/*
										@Override
										public void run() {
										// Log.e("轮询测试", "轮询测试"+i++);
										ArrayList<ImportFaultDwdotInfoSrvRequest> result = new ArrayList<ImportFaultDwdotInfoSrvRequest>();

										FWSDatabaseUtil util = new FWSDatabaseUtil(PollingService.this);
										util.open();

										Cursor cursor = util.select("mobile_eoms_fault_scene_data");
										if (cursor != null) {
										while (cursor.moveToNext()) {
										ImportFaultDwdotInfoSrvRequest importFaultDwdotInfo = new ImportFaultDwdotInfoSrvRequest();

										importFaultDwdotInfo.setMainId(cursor.getString(cursor.getColumnIndex("mainId")));
										importFaultDwdotInfo.setIsCentralize(cursor.getString(cursor.getColumnIndex("isCentralize")));
										importFaultDwdotInfo.setSheetId(cursor.getString(cursor.getColumnIndex("sheetId")));
										importFaultDwdotInfo.setOperateUserId(cursor.getString(cursor.getColumnIndex("operateUserId")));
										importFaultDwdotInfo.setOperateDeptId(cursor.getString(cursor.getColumnIndex("operateDeptId")));
										importFaultDwdotInfo.setOperateContact(cursor.getString(cursor.getColumnIndex("operateContact")));
										importFaultDwdotInfo.setOperateRoleId(cursor.getString(cursor.getColumnIndex("operateRoleId")));
										importFaultDwdotInfo.setOperateTime(DateUtil.stringToDateTimeC(cursor.getString(cursor
										.getColumnIndex("operateTime"))));
										importFaultDwdotInfo.setOperateType(cursor.getString(cursor.getColumnIndex("operateType")));
										importFaultDwdotInfo.setTaskId(cursor.getString(cursor.getColumnIndex("taskId")));
										importFaultDwdotInfo.setLongitude(cursor.getString(cursor.getColumnIndex("longitude")));
										importFaultDwdotInfo.setLatitude(cursor.getString(cursor.getColumnIndex("latitude")));
										importFaultDwdotInfo.setIsJob(cursor.getString(cursor.getColumnIndex("isjob")));
										importFaultDwdotInfo.setFaultDuration(cursor.getString(cursor.getColumnIndex("faultDuration")));
										importFaultDwdotInfo.setReserved1(cursor.getString(cursor.getColumnIndex("reserved1")));
										importFaultDwdotInfo.setReserved2(cursor.getString(cursor.getColumnIndex("reserved2")));
										importFaultDwdotInfo.setReserved3(cursor.getString(cursor.getColumnIndex("reserved3")));
										importFaultDwdotInfo.setReserved4(cursor.getString(cursor.getColumnIndex("reserved4")));
										importFaultDwdotInfo.setReserved5(cursor.getString(cursor.getColumnIndex("reserved5")));
										result.add(importFaultDwdotInfo);

										}
										}
										util.closeDb();

										handler.sendMessage(handler.obtainMessage(100, result));
										}
										*/
	}

	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {

			switch (msg.what) {/*
								case 100:
								ArrayList<ImportFaultDwdotInfoSrvRequest> result = (ArrayList<ImportFaultDwdotInfoSrvRequest>) msg.obj;
								if (result != null && result.size() > 0) {
								for (int i = 0; i < result.size(); i++) {
								new SubmitTask(PollingService.this, result.get(i)).execute();
								}
								} else if (result == null || result.size() == 0) {
								Log.i("执行完成", "关闭轮询");
								// 判断轮询服务是否存在，如果存在则关闭
								if (!PollingUtils.isWorked(PollingService.this, PollingService.ACTION)) {
								PollingUtils.stopPollingService(PollingService.this, PollingService.class,
								PollingService.ACTION);
								}
								}
								break;
								default:
								break;
								*/
			}

		}
	};

	/*
	private class SubmitTask extends AsyncTask<Void, Void, CommonSheetResponse> {

		private Context context;
		private ImportFaultDwdotInfoSrvRequest input;

		public SubmitTask(Context context, ImportFaultDwdotInfoSrvRequest input) {
			this.context = context;
			this.input = input;

		}

		@Override
		protected void onPreExecute() {
		}

		@Override
		protected CommonSheetResponse doInBackground(Void... params) {

			CommonSheetResponse response = new CommonSheetResponse();
			boolean flag = NetworkUtil.isConnectInternet(PollingService.this);
			if (flag) {
				try {
					response = new CommonSheetResponse();
					response.setServiceFlag("TRUE");
					ServiceUtils.initClient();

					response = ServiceUtils.getBO(IFaultSheetProvideSrv.class).importFaultDwdotInfoSrv(mhr, input);
					Log.i("调用接口后 response finish ", response.getServiceFlag() + "  " + response);
					Log.i("调用接口后 response ", response.getServiceFlag() + "  " + response.getServiceMessage());
				} catch (java.lang.reflect.UndeclaredThrowableException ex) {
					String message = ex.getCause().getMessage();
					if (message != null && message.equals(Constant.CONNETION_TIME_OUT)) {
						response.setServiceFlag("FALSE");
						response.setServiceMessage(Constant.CONNETION_TIME_OUT);
					} else if (message != null && message.equals(Constant.SERVER_ERR)) {
						response.setServiceFlag("FALSE");
						response.setServiceMessage(Constant.SERVER_ERR);
					} else {
						response.setServiceFlag("FALSE");
						response.setServiceMessage(Constant.NET_ERR);
					}
				} catch (Exception ex) {
					response.setServiceFlag("FALSE");
					response.setServiceMessage(Constant.NET_ERR);
				}
			} else {
				response.setServiceFlag("FALSE");
				response.setServiceMessage("没有网络");
			}
			return response;
		}

		@Override
		protected void onPostExecute(CommonSheetResponse result) {
			Log.i("result.getServiceFlag()==", "result.getServiceFlag()++" + result.getServiceFlag());
			Log.i("result.getServiceMessage()==", "result.getServiceMessage()++" + result.getServiceMessage());
			if (result.getServiceFlag().equals("TRUE")
					|| (result.getServiceFlag().equals("FALSE") && result.getServiceMessage().equals("已打点且无法重复打点"))) {
				FWSDatabaseUtil util = new FWSDatabaseUtil(PollingService.this);
				util.open();
				util.delete("mobile_eoms_fault_scene_data", input.getMainId(), Integer.valueOf(input.getOperateType()),
						input.getTaskId());
				util.closeDb();
			}
		}
	}*/

	@Override
	public void onDestroy() {
		super.onDestroy();
		System.out.println("Service:onDestroy");
	}

}
